﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestApp.BookStore.Authors;
using TestApp.BookStore.Books;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;

namespace TestApp.BookStore
{
    public class BookStoreDataSeeder : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<Book, Guid> _bookRepository;
        private readonly IAuthorRepository _authorRepository;
        private readonly AuthorManager _authorManager;

        public BookStoreDataSeeder(IRepository<Book, Guid> bookRepository,
            IAuthorRepository authorRepository,
            AuthorManager authorManager)
        {
            _bookRepository = bookRepository;
            _authorRepository = authorRepository;
            _authorManager = authorManager;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            // Book仓库中如果没有则添加种子数据
            if (await _bookRepository.GetCountAsync() <= 0)
            {
                // 插入第一本书
                await _bookRepository.InsertAsync(
                    new Book
                    {
                        Name = "格列佛游记", // 书名
                        Type = BookType.Dystopia, // 书的类型
                        PublishDate = new DateTime(1726, 01, 12), // 发布日期
                        Price = 19.84f // 价格
                    },
                    autoSave: true // 自动保存
                );

                // 插入第二本书
                await _bookRepository.InsertAsync(
                    new Book
                    {
                        Name = "三体", // 书名
                        Type = BookType.ScienceFiction,
                        PublishDate = new DateTime(2006, 05, 01), // 发布日期
                        Price = 42.0f
                    },
                    autoSave: true // 自动保存
                );
            }

            // Author仓库中如果没有则添加种子数据
            if (await _authorRepository.GetCountAsync() <= 0)
            {
                await _authorRepository.InsertAsync(
                    await _authorManager.CreateAsync(
                        "乔纳森·斯威夫特", // 作者名
                        new DateTime(1667, 11, 30), // 出生日期
                        "主人公格列佛到过的大人国、小人国、飞岛、巫人国、马国等离奇的国度，都带有强烈的乌托邦或反乌托邦色彩。" // 简介
                    )
                );

                await _authorRepository.InsertAsync(
                    await _authorManager.CreateAsync(
                        "刘慈欣",
                        new DateTime(1963, 06, 23),
                        "地球人类文明和三体文明的信息交流、生死搏杀及两个文明在宇宙中的兴衰历程。"
                    )
                );
            }
        }
    }
}